#!/usr/bin/python

import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


def send_mail(file_path):
    # 账号信息
    account = "xiaodong32@staff.weibo.com"
    password = str(os.environ.get('password'))
    receivers = str(os.environ.get('receivers'))
    to_addrs = receivers.split(",")
    print("构建参数account:", account)
    print("构建参数password:", password)
    print("构建参数to_addrs:", to_addrs)
    # 构建正文
    content = """
    附件全部开关
    """
    # 构造附件
    attachment = MIMEText(open(file_path, 'rb').read(), 'base64', 'utf-8')
    attachment['Content-Type'] = 'application/octet-stream'
    attachment['Content-Disposition'] = 'attachment;filename="result.csv"'
    # 构造消息体
    message = MIMEMultipart()
    message['From'] = account
    message['To'] = receivers
    message['Subject'] = 'iOS已全量开关还存在代码的开关'
    message.attach(MIMEText(content, 'plain', 'utf-8'))
    message.attach(attachment)
    # 邮件配置
    smtpObj = smtplib.SMTP('mail.staff.sina.com.cn')
    smtpObj.login(account, password)
    err = smtpObj.sendmail(account, to_addrs, message.as_string())
    if err:
        print("邮件发送失败")
    else:
        print("邮件发送成功")


if __name__ == '__main__':
    filename = "/Users/Sheldon/Workspace/Python/python/ScanAB/allonline.csv"
    if os.path.exists(filename):
        send_mail(file_path=filename)
    else:
        print("文件不存在")
